package com.hd.assistant.biz.service.impl;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.hd.assistant.biz.service.PaiPaiIsvService;
import com.hd.assistant.biz.service.exception.ServiceException;
import com.hd.assistant.biz.service.query.PaipaiItemSearchQuery;
import com.hd.assistant.biz.util.OpenApiException;
import com.hd.assistant.biz.util.PaiPaiOpenApiOauth;
import com.hd.assistant.biz.util.PaipaiUtil;
import com.hd.assistant.dal.dataobject.User;

public class PaipaiIsvServiceImpl implements PaiPaiIsvService{

	private final static Logger log = LoggerFactory.getLogger(PaipaiIsvServiceImpl.class);
	public JSONObject loadUserInfo(User user) throws ServiceException {
		if(!checkUser(user)){
			throw new ServiceException("用户信息不完整！");
		}
		PaiPaiOpenApiOauth sdk = generatePaipaiSevice(user);
		HashMap<String, Object> params = sdk.getParams(USER_INFO_METHOD);
		params.put("userUin", user.getUserId()+"");
		
		try {
			
			JSONObject jsonObj =sdk.invokeJson();
			return jsonObj;
			
		} catch (OpenApiException e) {
			log.error("Request Failed. code:%d, msg:%s"+e.getErrorCode()+e.getMessage());
			throw new ServiceException("http invok paipai error:",e);
		}
	}
	public JSONObject loadSellerItems(PaipaiItemSearchQuery query,User user)
			throws ServiceException {
		PaiPaiOpenApiOauth sdk = generatePaipaiSevice(user);
		query.setSellerUin(user.getUserId());
		HashMap<String, Object> params = sdk.getParams(ITEM_QUERY_METHOD);
		params.putAll(query.generateParams());
		try {
			
			JSONObject jsonObj =sdk.invokeJson();
			return jsonObj;	
		} catch (OpenApiException e) {
			log.error("Request Failed. code:%d, msg:%s"+e.getErrorCode()+e.getMessage());
			throw new ServiceException("http invok paipai error:",e);
		}
	}
	private boolean checkUser(User u){
		if(u == null)
			return false;
		if(u.getCurrentOAuthId() == null || u.getCurrentToken() == null || u.getUserId()==null)
			return false;
		return true;
	}

	private PaiPaiOpenApiOauth generatePaipaiSevice(User u){
		PaiPaiOpenApiOauth sdk = null;
		sdk = new PaiPaiOpenApiOauth(u.getCurrentOAuthId(), PaipaiUtil.oauthKeysMap.get(u.getCurrentOAuthId()), u.getCurrentToken(), u.getUserId());
		sdk.setCharset("gbk");
		sdk.setFormat("json");
		return sdk;
	}
	public JSONObject loadItemDetail(String itemCode,User user) throws ServiceException {
		PaiPaiOpenApiOauth sdk = generatePaipaiSevice(user);
		HashMap<String, Object> params = sdk.getParams(ITEM_DETAIL_METHOD);
		PaipaiItemSearchQuery query = new PaipaiItemSearchQuery();
		query.setItemCode(itemCode);
		params.putAll(query.generateParams());
		
		try {
			
			JSONObject jsonObj =sdk.invokeJson();
			return jsonObj;
			
		} catch (OpenApiException e) {
			log.error("Request Failed. code:%d, msg:%s"+e.getErrorCode()+e.getMessage());
			throw new ServiceException("http invok paipai error:",e);
		}
	}
	
}
